﻿/*
Copyright (c) 2008 William Duff

Permission is hereby granted, free of charge, to any person obtaining a copy 
of this software and associated documentation files (the "Software"), to deal 
in the Software without restriction, including without limitation the rights 
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 
copies of the Software, and to permit persons to whom the Software is furnished
to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all 
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 
SOFTWARE.
*/

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using LiveUpload.Facebook.Properties;

namespace LiveUpload.Facebook
{
    public partial class FacebookAccountRadioButton : RadioButton
    {
        #region Constructors

        /// <summary>
        /// Constructs a new FacebookAccountRadioButton to represent an account that can be 
        /// selected as part of the upload process.
        /// </summary>
        public FacebookAccountRadioButton()
        {
            InitializeComponent();

            // Radio buttons don't handle double clicks by default, so we set the style manually.
            SetStyle(ControlStyles.StandardClick, true);
            SetStyle(ControlStyles.StandardDoubleClick, true);
        }

        #endregion

        #region Private Fields

        private Account user;

        #endregion

        #region Public Fields

        /// <summary>
        /// The user that this radio button represents.
        /// </summary>
        public Account User
        {
            get { return this.user; }
            set 
            {
                if (this.user != value)
                {
                    this.user = value;
                    OnAccountChanged(new EventArgs());
                }
            }
        }

        /// <summary>
        /// Fires an event when the remove button is clicked.
        /// </summary>
        public event EventHandler RemoveClicked;

        #endregion

        #region Protected Fields

        /// <summary>
        /// This event is fired when the remove button is clicked.
        /// </summary>
        protected virtual void OnRemoveClicked(EventArgs e)
        {
            if (RemoveClicked != null)
            {
                RemoveClicked(this, e);
            }
        }

        #endregion

        #region Private Fields

        /// <summary>
        /// This event is fired when the account associated with this radio button is changed.
        /// </summary>
        private void OnAccountChanged(EventArgs eventArgs)
        {
            if (this.user != null)
            {
                // Set a tool tip on the name label in case it auto-ellipsises.
                this.facebookAccountToolTip.Active = true;
                this.facebookAccountToolTip.SetToolTip(this.facebookAccountName, this.user.Name);

                // Display the user name and network.
                this.facebookAccountName.Text = this.user.Name;
                this.facebookAccountNetworkName.Text = this.user.Network;

                // Asynchronously load the user's profile picture.
                if (!string.IsNullOrEmpty(this.user.SquarePictureUrl))
                {
                    this.facebookAccountSquarePicture.LoadAsync(this.user.SquarePictureUrl);
                }

                // Show any hidden elements.
                this.facebookAccountRemove.Show();
                this.facebookAccountBorderMiddle.Show();
                this.facebookAccountNetworkName.Show();
            }
        }

        /// <summary>
        /// This event is fired when a child control is clicked on.
        /// </summary>
        private void childControl_Click(object sender, EventArgs e)
        {
            OnClick(e);

            // Make sure this radio button gets selected as a result of the click.
            Select();
        }

        /// <summary>
        /// This event is fired when a child control is double clicked on.
        /// </summary>
        private void childControl_DoubleClick(object sender, EventArgs e)
        {
            OnDoubleClick(e);
        }

        /// <summary>
        /// This event is fired when a mouse enters the area of a child control.
        /// </summary>
        private void childControl_MouseEnter(object sender, EventArgs e)
        {
            OnMouseEnter(e);
        }

        /// <summary>
        /// This event is fired when a user clicks on the remove button.
        /// </summary>
        private void facebookAccountRemove_Click(object sender, EventArgs e)
        {
            OnRemoveClicked(e);
        }

        /// <summary>
        /// This event is fired when a mouse enters the area of the remove button.
        /// </summary>
        private void facebookAccountRemove_MouseEnter(object sender, EventArgs e)
        {
            // Show a hover image.
            this.facebookAccountRemove.Image = Resources.Delete_Hover;
        }

        /// <summary>
        /// This event is fired when a mouse leaves the area of the remove button.
        /// </summary>
        private void facebookAccountRemove_MouseLeave(object sender, EventArgs e)
        {
            // Go back to the original image.
            this.facebookAccountRemove.Image = Resources.Delete;
        }

        #endregion
    }
}